package com.ztc.ZtcDistributionRatio.web;
import java.util.HashMap;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ztc.ZtcDistributionRatio.bean.ZtcDistributionRatio;
import com.ztc.ZtcDistributionRatio.service.ZtcDistributionRatioManager;
@Controller
@RequestMapping("ztc_distribution_ratio_action")
public class ZtcDistributionRatioAction{
    @Autowired
    private ZtcDistributionRatioManager  ztcDistributionRatioManager;
    private static final Logger log = LogManager.getLogger(ZtcDistributionRatio.class);
    @RequestMapping("sign")
	@ResponseBody
	public HashMap<String, Object> save(String firstLevelManageRatio,String secondLevelManageRatio,String thirdLevelManageRatio,
			String firstLevelMerchantRatio,String secondLevelMerchantRatio,String firstLevelDistributionRatio,
			String secondLevelDistributionRatio,String thirdLevelDistributionRatio,String applicableCondition) {
		log.info("开始添加");
		HashMap<String, Object> wapper = new HashMap<>();
		ZtcDistributionRatio ztcDistributionRatio = generateZtcDistributionRatio(firstLevelManageRatio,
				secondLevelManageRatio, thirdLevelManageRatio, firstLevelMerchantRatio, secondLevelMerchantRatio,
				firstLevelDistributionRatio, secondLevelDistributionRatio, thirdLevelDistributionRatio,
				applicableCondition);
		String id = ztcDistributionRatioManager.save(ztcDistributionRatio);
		if (id == null){
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "重复添加");
			log.info("重复添加");
			return wapper;
		}else{
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "添加成功");
			log.info("添加成功");
			return wapper;
		}
	}

	private ZtcDistributionRatio generateZtcDistributionRatio(String firstLevelManageRatio,
			String secondLevelManageRatio, String thirdLevelManageRatio, String firstLevelMerchantRatio,
			String secondLevelMerchantRatio, String firstLevelDistributionRatio, String secondLevelDistributionRatio,
			String thirdLevelDistributionRatio, String applicableCondition) {
		ZtcDistributionRatio ztcDistributionRatio=new ZtcDistributionRatio();
		ztcDistributionRatio.setFirstLevelManageRatio(Float.parseFloat(firstLevelManageRatio));
		ztcDistributionRatio.setSecondLevelManageRatio(Float.parseFloat(secondLevelManageRatio));
		ztcDistributionRatio.setThirdLevelManageRatio(Float.parseFloat(thirdLevelManageRatio));
		ztcDistributionRatio.setFirstLevelMerchantRatio(Float.parseFloat(firstLevelMerchantRatio));
		ztcDistributionRatio.setSecondLevelMerchantRatio(Float.parseFloat(secondLevelMerchantRatio));
		ztcDistributionRatio.setFirstLevelDistributionRatio(Float.parseFloat(firstLevelDistributionRatio));
		ztcDistributionRatio.setSecondLevelDistributionRatio(Float.parseFloat(secondLevelDistributionRatio));
		ztcDistributionRatio.setThirdLevelDistributionRatio(Float.parseFloat(thirdLevelDistributionRatio));
		ztcDistributionRatio.setApplicableCondition(Integer.parseInt(applicableCondition));
		return ztcDistributionRatio;
	}

	@RequestMapping("init")
	@ResponseBody
	public HashMap<String, Object> getAll() {
		List<ZtcDistributionRatio> lists = ztcDistributionRatioManager.getAll();
		HashMap<String, Object> wapper = new HashMap<>();
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", lists);
		return wapper;
	}

	@RequestMapping("del")
	@ResponseBody
	public HashMap<String, Object> delete(String id) {
		boolean isTrue = ztcDistributionRatioManager.delete(id);
		HashMap<String, Object> wapper = new HashMap<>();
		if (isTrue) {
			wapper.put("success", Boolean.valueOf(true));
			wapper.put("data", "删除成功");
		} else {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "操作失败");
		}
		return wapper;
	}

	@RequestMapping("searchInfoEquals")
	@ResponseBody
	public HashMap<String, Object> searchInfoEquals(String applicableCondition) {
		List<ZtcDistributionRatio> lists = ztcDistributionRatioManager.searchInfoEquals(applicableCondition);
		HashMap<String, Object> wapper = new HashMap<>();
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", lists);
		return wapper;
	}

	@RequestMapping("update")
	@ResponseBody
	public HashMap<String, Object> update(String firstLevelManageRatio,String secondLevelManageRatio,String thirdLevelManageRatio,
			String firstLevelMerchantRatio,String secondLevelMerchantRatio,String firstLevelDistributionRatio,
			String secondLevelDistributionRatio,String thirdLevelDistributionRatio,String applicableCondition,String id) {
		log.info("开始修改");
		HashMap<String, Object> wapper = new HashMap<>();
		ZtcDistributionRatio distributionRatio=generateZtcDistributionRatio(firstLevelManageRatio, secondLevelManageRatio, thirdLevelManageRatio, firstLevelMerchantRatio, secondLevelMerchantRatio, firstLevelDistributionRatio, secondLevelDistributionRatio, thirdLevelDistributionRatio, applicableCondition);
		distributionRatio.setId(id);
		String result=ztcDistributionRatioManager.update(distributionRatio);
		if("1".equals(result)){
			wapper.put("success", Boolean.valueOf(true));
			wapper.put("data", "修改成功");
			log.info("修改成功");
		}else{
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "修改失败");
			log.info("修改失败");
		}
		return wapper;
	}
}